import inspect
import pydirectinput as pdi
import pyautogui as pyg
import pygetwindow as gw
import keyboard
import time 
import os
pause=False 
ACTION_NAME="深渊"

DNF_WD = gw.getWindowsWithTitle("Dungeon & Fighter")[0]
STATUS_FIND_CLICK = 101  # 找到图片并点击
STATUS_FIND = 102  # 只找图片
STATUS_NOT_IMG = 104  # 图片不存在
STATUS_NOT_FOUND = 404  # 没找到图片
#gw.activate()
#print(f"title:{DNF_WD}")
def get_image_path(image_name:str):
    return os.path.join(os.path.join(os.path.dirname(__file__),"image"),image_name)
DNF_REGION=(DNF_WD.left,DNF_WD.top,DNF_WD.left + DNF_WD.width, DNF_WD.top+DNF_WD.height)
INIT_WIDTH=800
INIT_HEIGHT=600
def compute_position(x_offset,y_offset):
    y= DNF_WD.top+(INIT_HEIGHT/2+y_offset)*(DNF_WD.height/INIT_HEIGHT)
    x= DNF_WD.left+(INIT_WIDTH/2+x_offset)*(DNF_WD.width/INIT_WIDTH)
    return (x,y)
def get_position():
    try:
        # 获取当前鼠标的坐标
        x, y = pyg.position()
        print(f"Mouse position: X={x}, Y={y}")
        time.sleep(1)
    except KeyboardInterrupt:
        print("Program terminated.")

def click_dnf(x,y):
    pyg.moveTo(x,y,duration=0.3)
    pyg.mouseDown(button="left")
    pyg.mouseUp(button="left")

def pause():
    global pause 
    pause =  True
    print(f"暂停{ACTION_NAME}")
def start():
    global pause 
    pause =  False
    print(f"开始{ACTION_NAME}")
def move():
    pyg.keyDown("left")

# 定义一个函数来模拟长按右键
def hold_key(duration,key):
    try:
        # 按下右方向键
        pdi.keyDown(key)
        # 等待指定的时间
        time.sleep(duration)
    finally:
        # 释放右方向键
        pdi.keyUp(key)
def running(duration,key):
    try:
        # 按下右方向键
        pdi.press(key)
        pdi.keyDown(key)
        # 等待指定的时间
        time.sleep(duration)
    finally:
        # 释放右方向键
        pdi.keyUp(key)
# 识图封装 300找图,，点一次，相似度0.9，像素步长5,不灰度
def find_img_click(path, count=10, clicks=1, confidence=0.9, region=(0, 0, 800, 600), grayscale=False, step=5):
    if not os.path.exists(path):
        print(f"文件 {path.split('/')[-1]} 不存在")
        return STATUS_NOT_IMG
    attempts = 1
    while attempts < count+1:
        try:
            start_time = time.time()
            point = pyg.locateCenterOnScreen(
                path, confidence=confidence, region=region, grayscale=grayscale, step=step)
            end_time = time.time()
            print(f"第{attempts}次 {path.split('/')[-1]} 识别成功 无点击耗时{end_time-start_time:.3f}，region:{region}")
            return STATUS_FIND
        except pyg.ImageNotFoundException:
            not_find_time = time.time()
            print(f"第{attempts}次 {path.split('/')[-1]} 识别失败 耗时{not_find_time-start_time:.3f}，region:{region}")
        attempts += 1
    if attempts >= count:
        print("未发现图片")
    return STATUS_NOT_FOUND
# 识图错误 打印，发送

def into_shenyuan():
    # 进图
    print(f"开始进入{ACTION_NAME}")
    hold_key(2,"right")
    time.sleep(1)
    pdi.press("f11")
    xbu_image=get_image_path("xbu.png")
    is_xbu = find_img_click(xbu_image,region=DNF_REGION)
    if not is_xbu:
        pdi.press("up")
    if find_img_click(xbu_image,region=DNF_REGION):
        pdi.press("right")
        pdi.press("right")
        pdi.press("right")
        pdi.press("right")
        pdi.press("space")
    else :
        print("未发现西部图片")
    time.sleep(2.5)
    
def house_wrap(name,house):
    if pause:
        return True
    print(f"开始第{name}个房间")
    into_image=get_image_path(f"{name}.png")
    end_image=get_image_path("2-end.png")
    while not find_img_click(into_image, region=DNF_REGION):
        time.sleep(0.2)
    house()
    time.sleep(1)
    running(0.2,"right")
    while not find_img_click(end_image,region=DNF_REGION):
        running(0.1,"right")
    running(0.2,"right")    
    

def house1():
    hold_key(0.3,"down")
    hold_key(0.1,"up")
    pdi.press("y")
    pdi.press("t")
    pdi.press("d")
    running(0.7,"right")
    pdi.press("q")


def house2():
    running(0.1,"right")
    pdi.press("d")
    time.sleep(0.5)
    pdi.press("a")

def house3():
    hold_key(0.05,"right")
    hold_key(0.05,"down")
    running(0.05,"right")
    pdi.press("d")
    time.sleep(0.5)
    pdi.press("a")
    #time.sleep(0.5)
   
def house4():
    pdi.press("d")
    time.sleep(0.5)
    pdi.press("a")
    running(0.6,"right")
    pdi.press("d")
    time.sleep(0.5)
    pdi.press("a")


def house5():
    time.sleep(2)
    running(0.1,"right")
    pdi.press("d")
    #hold_key(0.1,"down")
    time.sleep(1.5)
    pdi.press("d")
    time.sleep(0.3)
    pdi.press("c")
    #running(0.5,"right")
    pdi.press("d")
    pdi.press("left")
    time.sleep(1)
    pdi.press("q")
    time.sleep(0.5)
    pdi.press("a")
    time.sleep(0.5)
    pdi.press("f")
    time.sleep(5)
    pdi.keyDown("alt")
    pdi.keyDown("s")
    pdi.keyUp("s")
    pdi.keyUp("alt")
    pdi.keyDown("alt")
    pdi.keyDown("w")
    pdi.keyUp("w")
    pdi.keyUp("alt")
    running(0.5,"right")
    

def house6():
    hold_key(0.01,"up")
    pdi.press("d")
    running(0.5,"right")
    pdi.press("d")
    hold_key(0.02,"down")
def house7():
    pdi.press("d")


def shenyuan():
    global pause
    cnt = 1
    while True: 
        if pause:
            time.sleep(1)
            cnt=1
        else:
            if cnt <= 1:
                print("开始深渊")
                into_shenyuan()
            house_wrap("1",house1)
            house_wrap("2",house2)
            house_wrap("3",house3)
            house_wrap("4",house4)
            house_wrap("5",house5)
            house_wrap("6",house6)
            house_wrap("7",house7)
            #pause=True
            if pause == False :
                time.sleep(10)
                pdi.press("esc")
                time.sleep(2)
                pdi.press("f10")
                time.sleep(10)
            if cnt>13:
                #回城吃疲劳  
                if pause == False :
                    time.sleep(10)
                    pdi.press("F12")
                    time.sleep(5)
                    pdi.press("3")
                    into_shenyuan()    
            cnt=cnt+1


keyboard.add_hotkey("ctrl+f5",start)
keyboard.add_hotkey("ctrl+f6",pause)
print(f"1,请按ctrl+f5开始 ，ctrl+f6停止{ACTION_NAME}")
shenyuan()

        
